/**
 * Created by 荆安喜 on 2017/9/25.
 */
$(function () {
    var ad = appData(),
        pageNumNow = 1,
        paramsT = {},
        dataPage

    var temp = {
        page: "{{each(i,val) content}}\n<td>{{= val.num}}</td>\n<td>{{= val.name}}</td>\n<td class=\"iptxt\">\n    {{each(i,val) val.ips}}\n    <span>{{= val}}</span>\n    {{/each}}\n</td>\n<td>{{= val.updateTime}}</td>\n<td class=\"icons\"><a href=\"#\" title=\"修改\"><i class=\"icon-setting\"></i></a><a href=\"#\" title=\"删除\"><i class=\"icon-delete\"></i></a></td>\n{{/each}}"
    }


    //初始化事件加载
    function event() {
        $("#add").on("click", function () {
            $('.dialog-head span').attr('data-id','')
            $(".dialog").show();
            $('#ipName').val('')
            $('#ipText').val('')
        })

        $(".dialog .close,#cancel").on("click", function () {
            $(".dialog").hide();
        })
    }


    $(function () {
        event()

        $('#save').on('click', addIp)

        $('#searchIp').on('click',searchIp)
        //分页初始化
        paramsT.keyword=$('#searchText').val()
        showTable(true, 1, 'KMGA_PAGE_IP', 'ipPage')
    })


    //分页信息
    function showTable(dontRenderPager, page, url, domId) {
        paramsT.page = page
        paramsT.pageSize = 10
        ad.selectList(url, paramsT, function (data) {
            $("#" + domId).empty();
            if (data.content.length === 0) {
                $("#" + domId).append("<tr><td colspan='4'  class='colspanCss'><h1>暂无数据</h1></td></tr>");
                $("#pageList").empty();
                return;
            }

            if (data.content <= appApi("PAGE_SIZE")) {
                $("#pageList").empty();
                return;
            }
            $.get('scripts/app/safe/dataRegulation/ipPage.tmpl', function (tmpl) {
                $("#" + domId).empty();
                dataPage = data
                $.tmpl(tmpl, data).appendTo("#" + domId);
                //配置事件
                $('.icon-setting').on('click', deploy)
                //删除事件
                $('.icon-delete').on('click', delAlarm)
                //table 排序插件
                $('table').tablesort().data('tablesort');
                //input效果 批量删除注册
                var bat = new batch('checkAll')
                bat.delete('delAll', batchDel)

                if (dontRenderPager) {
                    renderPager(data.totalRecord);
                }
            })

        });
    }

    function renderPager(totalRecord, nowPage) {
        $("#pageList").pagination(totalRecord, appApi("PAGE_SIZE"), {
            callback: function (page_id, jq) {
                pageNumNow = page_id + 1;
                showTable(false, pageNumNow, 'KMGA_PAGE_IP', 'ipPage');
            }
        }, nowPage);
    }


    function addIp() {
        var id = $('.dialog-head span').attr('data-id')
        var parms = {
            name: $('#ipName').val(),
            ip: $('#ipText').val(),
            id: id
        }
        if(id==''){
            ad.insert('KMGA_ADD_IP', parms, function () {
                $('#ipName').val('')
                $('#ipText').val('')
                showTable(true, pageNumNow, 'KMGA_PAGE_IP', 'ipPage');
                $(".dialog").hide();
            })
        }else {
           ad.update('KMGA_UPDATA_IP',parms,function () {
               $('#ipName').val('')
               $('#ipText').val('')
               showTable(true, pageNumNow, 'KMGA_PAGE_IP', 'ipPage');
               $(".dialog").hide();
           })
        }

    }

    //批量删除事件
    function batchDel(parms) {
        ad.insert('KMGA_DELALL_IP', parms, function () {
            showTable(false, pageNumNow, 'KMGA_PAGE_IP', 'ipPage');
            $('#checkAll').iCheck('uncheck')
            _as.jAlert("<p>删除成功</p>")
        })
    }

    //配置页面
    function deploy() {
        var id = this.parentElement.dataset.id
        dataPage.content.forEach(function (d, i) {
            if (d.id == id) {
                $('#ipName').val(d.name)
                $('#ipText').val(d.ip)
            }
        })
        $('.dialog-head span').html('修改IP')
        $('.dialog-head span').attr('data-id', id)
        $(".dialog").show()
    }

    //删除事件
    function delAlarm() {
        var id = this.parentElement.dataset.id
        _as.jConfirm('确定是否要删除',function(){
            ad.del('KMGA_DEL_IP', {id: id}, function () {
                showTable(false, pageNumNow, 'KMGA_PAGE_IP', 'ipPage');
            })
        })

    }

    //检索
     function searchIp() {
         paramsT.keyword=$('#searchText').val()
         showTable(true, 1, 'KMGA_PAGE_IP', 'ipPage')
     }
})